package template

import "fmt"

type Downloader interface {
	Download(uri string)
}
type template struct {
	implement
	uri string
}
type implement interface {
	download()
	save()
}

//魔板 template
// imple 具体的实现
// donwloadhttp uri
// donwloduri
// te,pleta
// imple
// dinw
// save
// temple
// impl
// Donwload uir
// t.uri =
// t.impl./do
//
func newTemplate(impl implement) *template {
	return &template{
		implement: impl,
	}
}
func (t *template) Download(uri string) {
	t.uri = uri
	fmt.Printf("prepare downloading\n")
	t.implement.download()
	t.implement.save()
	fmt.Print("finnish downloading\n")
}
func (t *template) save() {
	fmt.Print("default save\n")
}

type HttpDownloader struct {
	*template
}

func NewHttpDownloader() Downloader {
	downloader := &HttpDownloader{}
	template := newTemplate(downloader)
	downloader.template = template
	return downloader
}
func (d *HttpDownloader) download() {
	fmt.Printf("download %s via http\n", d.uri)
}
func (*HttpDownloader) save() {
	fmt.Printf("http save\n")
}

type FtpDownloader struct {
	*template
}

func NewFtfDownloader() Downloader {
	downloader := &FtpDownloader{}
	template := newTemplate(downloader)
	downloader.template = template
	return downloader
}
func (d *FtpDownloader) download() {
	fmt.Printf("download %s via ftp\n", d.uri)
}
